package handler;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.esen.util.GUID;
import com.mxgraph.model.mxCell;
import com.mxgraph.model.mxGeometry;
import com.mxgraph.util.mxRectangle;
import com.mxgraph.view.mxGraph;

public class DrawTable {
	
	private static Map<String,mxCell> cells = new HashMap<String,mxCell>();
	
	private final static int TABLE_WIDTH = 200;
	
	private final static int HEADER_HEIGHT = 44;
	
	private final static int ROW_HEIGHT = 36;
	
	private final static int IMGCELL_X = 10;
	
	private final static int IMGCELL_WIDTH = 40;
	
	private final static int IMGCELL_HEIGHT = 26;
	
	private final static int CAPTIONCELL_HEIGHT = 30;
	
	private final static int CAPTIONCELL_WIDTH = 40;
	
	private final static int CAPTIONCELL_WIDTH_X = 65;
	
	private final static int DESCCELL_HEIGHT = 30;
	
	private final static int DESCCELL_WIDTH = 70;
	
	private final static int DESCCELL_WIDTH_X = 120;
	
	private static String factStyle = "swimlane;whiteSpace=wrap;html=1;startSize=43;resizable=0;movable=1;rotatable=0;gradientColor=none;fillColor=#4270C5;gradientDirection=west;strokeColor=#A6BAD1;align=center;labelPosition=center;verticalLabelPosition=middle;verticalAlign=middle;";
	
	private static String tableHeader = "<h4 style=\"text-align: left; color: white;\">TableName<span style=\"font-weight: normal\">TableCaption</span></h4>";
	
	private static String primaryKeyStyle = "whiteSpace=wrap;html=1;resizable=0;movable=0;rotatable=0;clonable=0;image=../../../../images/id.png;strokeColor=none;fillColor=none;align=left;labelPosition=right;verticalLabelPosition=middle;verticalAlign=middle;";
	
	private static String commonCellStyle = "whiteSpace=wrap;html=1;resizable=0;movable=0;rotatable=0;strokeColor=none;fillColor=none;align=left;";

	private static String captionKeyStyle = "whiteSpace=wrap;html=1;resizable=0;movable=0;rotatable=0;clonable=0;image=../../../../images/id.png;strokeColor=none;fillColor=none;align=left;labelPosition=right;verticalLabelPosition=middle;verticalAlign=middle;";
	
	public static void drawTable(TableEntity table,mxGraph graph){
		
		tableHeader = "<h4 style=\"text-align: left;color: white;\">TableCaption</h4>";
		Object dparent = graph.getDefaultParent();
		mxCell tablecell = inserCell(graph,dparent);
		tablecell.setStyle(factStyle);
		String replace = tableHeader.replace("TableName", table.getTableName());
		String value = replace.replace("TableCaption", table.getTableCaption());
		tablecell.setValue(value);
		List<Column> columns = table.getColumns();
		int len = columns.size();
		double tableHeight = HEADER_HEIGHT + len *ROW_HEIGHT;
		mxGeometry geometry = tablecell.getGeometry();
		geometry.setHeight(tableHeight);
		mxRectangle alternateBounds = new mxRectangle();
		alternateBounds.setWidth(TABLE_WIDTH);
		alternateBounds.setHeight(HEADER_HEIGHT);
		geometry.setAlternateBounds(alternateBounds);
		for(int i=0;i<len;i++){
			Column col = columns.get(i);
				insertRow(col,i,graph,tablecell);
		}
	}
	
	/**
	 * 插入一条新行
	 * @param col
	 * @param index
	 * @param graph
	 * @param parent
	 */
	private static void insertRow(Column col, int index , mxGraph graph,mxCell parent){
		double y = index * ROW_HEIGHT + HEADER_HEIGHT;
		mxCell colCell = inserCell(graph, parent);
		colCell.setConnectable(false);
		mxGeometry colgeo = new mxGeometry(0,y,TABLE_WIDTH,ROW_HEIGHT);
		colCell.setStyle(commonCellStyle);
		colCell.setGeometry(colgeo);
		mxCell  imgCell = inserCell(graph,colCell);
		if(col.isPrimayKey()){
			 imgCell.setStyle(primaryKeyStyle);
			 mxCell sport1 = inserCell(graph, colCell);
			 mxGeometry sportget1 = new mxGeometry(0,ROW_HEIGHT-5,0,0);
			 sport1.setGeometry(sportget1);
			 sport1.setParent(colCell);
			 mxCell sport2 = inserCell(graph, colCell);
			 mxGeometry sportget2 = new mxGeometry(TABLE_WIDTH,ROW_HEIGHT-5,0,0);
			 sport2.setGeometry(sportget2);
			 sport2.setParent(colCell);
			 mxCell edge = (mxCell)graph.insertEdge(colCell, GUID.makeGuid(), "", sport1, sport2);
			 edge.setStyle("endArrow=none;dashed=1;html=1;strokeColor=#D9D9D9;");
		}else if(col.isCaptionKey()){
			 imgCell.setStyle(captionKeyStyle);
		}else{
			imgCell.setStyle(commonCellStyle);
		}
		mxGeometry imggeo = new mxGeometry(IMGCELL_X,0,IMGCELL_WIDTH,IMGCELL_HEIGHT);
		imgCell.setGeometry(imggeo);
		imgCell.setConnectable(false);
		mxCell captioncell = inserCell(graph,colCell);
		captioncell.setStyle(commonCellStyle);
		captioncell.setConnectable(false);
		mxGeometry captiongeo = new mxGeometry(CAPTIONCELL_WIDTH_X,0,CAPTIONCELL_WIDTH,CAPTIONCELL_HEIGHT);
		captioncell.setGeometry(captiongeo);
		captioncell.setValue(col.getCaption());
		mxCell descCell = inserCell(graph,colCell);
		mxGeometry descgeo = new mxGeometry(DESCCELL_WIDTH_X,0,DESCCELL_WIDTH,DESCCELL_HEIGHT);
		descCell.setConnectable(false);
		descCell.setGeometry(descgeo);
		descCell.setStyle(commonCellStyle);
		descCell.setValue(col.getDesc());
	}
	
	
	private static void insertBottomRow(){
		
	}
	
	private static mxCell inserCell(mxGraph graph,Object parent){
		mxCell cell = null;
		String makeGuid = GUID.makeGuid();
		cell = (mxCell)graph.insertVertex(parent, makeGuid,  null, 0, 0, TABLE_WIDTH, 0, null);//某个情况生成的cell在外部高度设置无效，所以取消这里的默认高度，原因不明
		cells.put(makeGuid, cell);
		return cell;
	}
}
